//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Bond.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/CashFlow.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instrument.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CBond::CBond (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, Double faceAmount, DateTime maturityDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ issueDate, Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Cephei::QL::ICashFlow^>^>^ cashflows, Cephei::QL::IPricingEngine^ QL_Pricer) : CInstrument(CBond::typeid)
{
    CCalendar^ _Ccalendar;
    try
    {
#ifdef HANDLE
        _phBond = NULL;
#endif
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays); //d
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::Real _faceAmount = (QuantLib::Real)ValueHelper::Convert (faceAmount); //d
        QuantLib::Date _maturityDate = (QuantLib::Date)ValueHelper::Convert (maturityDate); //d
        QuantLib::Date _issueDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (issueDate) ? (QuantLib::Date)ValueHelper::Convert (issueDate->Value) : QuantLib::Date()); //4
        CCashFlowVector^ _NCcashflows;
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Cephei::QL::ICashFlow^>^>::IsSome::get (cashflows))
        {
            cashflows->Value->Lock();
            INativeVector<Cephei::QL::ICashFlow^>^ _NCIcashflows = cashflows->Value->getFeature (NativeFeature::shared_ptr);
            _NCcashflows = safe_cast<CCashFlowVector^>(_NCIcashflows);
        }
        std::vector<boost::shared_ptr<QuantLib::CashFlow> >& _cashflows = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Cephei::QL::ICashFlow^>^>::IsSome::get (cashflows) ? static_cast<std::vector<boost::shared_ptr<QuantLib::CashFlow> >&> (_NCcashflows->GetShared ()) : QuantLib::Leg());//2
        _ppBond = new boost::shared_ptr<QuantLib::Bond> (new QuantLib::Bond ( _settlementDays,  _calendar,  _faceAmount,  _maturityDate,  _issueDate,  _cashflows ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppBond)->setPricingEngine (_QL_Pricer);
        SetInstrument (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppBond));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (cashflows != nullptr) cashflows->Value->Unlock();
    }
}
Cephei::QL::Instruments::CBond::CBond (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, Microsoft::FSharp::Core::FSharpOption<DateTime>^ issueDate, Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Cephei::QL::ICashFlow^>^>^ coupons, Cephei::QL::IPricingEngine^ QL_Pricer) : CInstrument(CBond::typeid)
{
    CCalendar^ _Ccalendar;
    try
    {
#ifdef HANDLE
        _phBond = NULL;
#endif
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays); //d
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::Date _issueDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (issueDate) ? (QuantLib::Date)ValueHelper::Convert (issueDate->Value) : QuantLib::Date()); //4
        CCashFlowVector^ _NCcoupons;
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Cephei::QL::ICashFlow^>^>::IsSome::get (coupons))
        {
            coupons->Value->Lock();
            INativeVector<Cephei::QL::ICashFlow^>^ _NCIcoupons = coupons->Value->getFeature (NativeFeature::shared_ptr);
            _NCcoupons = safe_cast<CCashFlowVector^>(_NCIcoupons);
        }
        std::vector<boost::shared_ptr<QuantLib::CashFlow> >& _coupons = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Cephei::QL::ICashFlow^>^>::IsSome::get (coupons) ? static_cast<std::vector<boost::shared_ptr<QuantLib::CashFlow> >&> (_NCcoupons->GetShared ()) : QuantLib::Leg());//2
        _ppBond = new boost::shared_ptr<QuantLib::Bond> (new QuantLib::Bond ( _settlementDays,  _calendar,  _issueDate,  _coupons ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppBond)->setPricingEngine (_QL_Pricer);
        SetInstrument (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppBond));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (coupons != nullptr) coupons->Value->Unlock();
    }
}
Cephei::QL::Instruments::CBond::CBond (boost::shared_ptr<QuantLib::Bond>& childNative, Object^ owner) : CInstrument(CBond::typeid)
{
#ifdef HANDLE
	_phBond = NULL;
#endif
	_ppBond = &childNative;
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppBond));
}
Cephei::QL::Instruments::CBond::CBond (QuantLib::Bond& childNative, Object^ owner) : CInstrument(CBond::typeid)
{
#ifdef HANDLE
	_phBond = NULL;
#endif
	_ppBond = new boost::shared_ptr<QuantLib::Bond> (&childNative);
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppBond));
    _BondOwner = owner;
    _InstrumentOwner = owner;
}

Cephei::QL::Instruments::CBond::CBond (CBond^ copy) : CInstrument(CBond::typeid)
{
#ifdef HANDLE
	_phBond = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBond = new boost::shared_ptr<QuantLib::Bond> (copy->GetShared());
        _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppBond));
    }
}
Cephei::QL::Instruments::CBond::CBond (PLATFORM::Type^ t) : CInstrument(CBond::typeid)
{
#ifdef HANDLE
	_phBond = NULL;
#endif
	if (!t->IsSubclassOf(CBond::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CBond::CBond (QuantLib::Handle<QuantLib::Bond>& childNative, Object^ owner)  : CInstrument(CBond::typeid)
{
	_phBond = &childNative;
	_ppBond = &static_cast<boost::shared_ptr<QuantLib::Bond>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppBond));
    _BondOwner = owner;
}
Cephei::QL::Instruments::CBond::CBond (QuantLib::Handle<QuantLib::Bond> childNative)  : CInstrument(CBond::typeid)
{
	_phBond = &childNative;
	_ppBond = &static_cast<boost::shared_ptr<QuantLib::Bond>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppBond));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CBond::CBond (QuantLib::Bond childNative)  : CInstrument(CBond::typeid)
{
#ifdef HANDLE
	_phBond = NULL;
#endif
	_ppBond = new boost::shared_ptr<QuantLib::Bond> (new QuantLib::Bond (childNative));
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppBond));
}
#endif

Cephei::QL::Instruments::CBond::~CBond ()
{
    if (_ppBond != NULL)
    {
	    delete _ppBond;
        _ppBond = NULL;
    }
}
Cephei::QL::Instruments::CBond::!CBond ()
{
    if (_ppBond != NULL)
    {
	    delete _ppBond;
    }
}
QuantLib::Bond& Cephei::QL::Instruments::CBond::GetReference ()
{
    if (_ppBond == NULL) throw REFNEW NativeNullException ();
	return **_ppBond;
}
boost::shared_ptr<QuantLib::Bond>& Cephei::QL::Instruments::CBond::GetShared ()
{
    if (_ppBond == NULL) throw REFNEW NativeNullException ();
	return *_ppBond;
}
QuantLib::Bond* Cephei::QL::Instruments::CBond::GetPointer ()
{
    if (_ppBond == NULL) throw REFNEW NativeNullException ();
	return &**_ppBond;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Bond>& Cephei::QL::Instruments::CBond::GetHandle ()
{
	if (_phBond == NULL)
	{
		_phBond = new Handle<QuantLib::Bond> (*_ppBond);
	}
	return *_phBond;
}
#endif
bool Cephei::QL::Instruments::CBond::HasNative () 
{
	return (_ppBond != NULL);
}

Double Cephei::QL::Instruments::CBond::AccruedAmount (Microsoft::FSharp::Core::FSharpOption<DateTime>^ d)
{
    try
    {
        QuantLib::Date _d = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (d) ? (QuantLib::Date)ValueHelper::Convert (d->Value) : QuantLib::Date()); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBond)->accruedAmount ( _d );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::ICalendar^ Cephei::QL::Instruments::CBond::Calendar::get ()
{
    try
    {
    	QuantLib::Calendar& _rv = (QuantLib::Calendar&)(*_ppBond)->calendar ( );   
        Cephei::QL::Times::CCalendar^ _nrv = REFNEW Cephei::QL::Times::CCalendar (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ Cephei::QL::Instruments::CBond::Cashflows::get ()
{
    try
    {
    	std::vector<boost::shared_ptr<QuantLib::CashFlow> >& _rv = (std::vector<boost::shared_ptr<QuantLib::CashFlow> >&)(*_ppBond)->cashflows ( );   
        CoVector<Cephei::QL::ICashFlow^>^ _nrv = REFNEW CoVector<Cephei::QL::ICashFlow^>(REFNEW CCashFlowVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CBond::CleanPrice (Double yield, Cephei::QL::Times::IDayCounter^ dc, QL::CompoundingEnum comp, QL::Times::FrequencyEnum freq, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate)
{
    CDayCounter^ _Cdc;
    try
    {
        QuantLib::Rate _yield = (QuantLib::Rate)ValueHelper::Convert (yield); //a
        _Cdc = safe_cast<CDayCounter^> (dc);
        _Cdc->Lock();
        QuantLib::DayCounter& _dc = static_cast<QuantLib::DayCounter&> (_Cdc->GetReference ()); 
        QuantLib::Compounding _comp = (QuantLib::Compounding)comp ;
        QuantLib::Frequency _freq = (QuantLib::Frequency)freq ;
        QuantLib::Date _settlementDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (settlementDate) ? (QuantLib::Date)ValueHelper::Convert (settlementDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBond)->cleanPrice ( _yield,  _dc,  _comp,  _freq,  _settlementDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cdc != nullptr) _Cdc->Unlock();
    }
}
Double Cephei::QL::Instruments::CBond::CleanPrice ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBond)->cleanPrice ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CBond::DirtyPrice (Double yield, Cephei::QL::Times::IDayCounter^ dc, QL::CompoundingEnum comp, QL::Times::FrequencyEnum freq, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate)
{
    CDayCounter^ _Cdc;
    try
    {
        QuantLib::Rate _yield = (QuantLib::Rate)ValueHelper::Convert (yield); //a
        _Cdc = safe_cast<CDayCounter^> (dc);
        _Cdc->Lock();
        QuantLib::DayCounter& _dc = static_cast<QuantLib::DayCounter&> (_Cdc->GetReference ()); 
        QuantLib::Compounding _comp = (QuantLib::Compounding)comp ;
        QuantLib::Frequency _freq = (QuantLib::Frequency)freq ;
        QuantLib::Date _settlementDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (settlementDate) ? (QuantLib::Date)ValueHelper::Convert (settlementDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBond)->dirtyPrice ( _yield,  _dc,  _comp,  _freq,  _settlementDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cdc != nullptr) _Cdc->Unlock();
    }
}
Double Cephei::QL::Instruments::CBond::DirtyPrice ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBond)->dirtyPrice ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Instruments::CBond::IsExpired::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppBond)->isExpired ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Instruments::CBond::IssueDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppBond)->issueDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Instruments::CBond::MaturityDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppBond)->maturityDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CBond::Notional (Microsoft::FSharp::Core::FSharpOption<DateTime>^ d)
{
    try
    {
        QuantLib::Date _d = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (d) ? (QuantLib::Date)ValueHelper::Convert (d->Value) : QuantLib::Date()); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBond)->notional ( _d );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Instruments::CBond::IsTradable (Microsoft::FSharp::Core::FSharpOption<DateTime>^ d)
{
    try
    {
        QuantLib::Date _d = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (d) ? (QuantLib::Date)ValueHelper::Convert (d->Value) : QuantLib::Date()); //9a
    	bool _rv = (bool)(*_ppBond)->isTradable ( _d );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Instruments::CBond::NextCashFlowDate (Microsoft::FSharp::Core::FSharpOption<DateTime>^ d)
{
    try
    {
        QuantLib::Date _d = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (d) ? (QuantLib::Date)ValueHelper::Convert (d->Value) : QuantLib::Date()); //9a
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppBond)->nextCashFlowDate ( _d );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CBond::NextCouponRate (Microsoft::FSharp::Core::FSharpOption<DateTime>^ d)
{
    try
    {
        QuantLib::Date _d = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (d) ? (QuantLib::Date)ValueHelper::Convert (d->Value) : QuantLib::Date()); //9a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppBond)->nextCouponRate ( _d );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Instruments::CBond::PreviousCashFlowDate (Microsoft::FSharp::Core::FSharpOption<DateTime>^ d)
{
    try
    {
        QuantLib::Date _d = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (d) ? (QuantLib::Date)ValueHelper::Convert (d->Value) : QuantLib::Date()); //9a
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppBond)->previousCashFlowDate ( _d );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Instruments::CBond::Notionals::get ()
{
    try
    {
    	std::vector<QuantLib::Real>& _rv = (std::vector<QuantLib::Real>&)(*_ppBond)->notionals ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::ICashFlow^ Cephei::QL::Instruments::CBond::Redemption::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::CashFlow>& _rv = (boost::shared_ptr<QuantLib::CashFlow>&)(*_ppBond)->redemption ( );   
        Cephei::QL::CCashFlow^ _nrv = REFNEW Cephei::QL::CCashFlow (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ Cephei::QL::Instruments::CBond::Redemptions::get ()
{
    try
    {
    	std::vector<boost::shared_ptr<QuantLib::CashFlow> >& _rv = (std::vector<boost::shared_ptr<QuantLib::CashFlow> >&)(*_ppBond)->redemptions ( );   
        CoVector<Cephei::QL::ICashFlow^>^ _nrv = REFNEW CoVector<Cephei::QL::ICashFlow^>(REFNEW CCashFlowVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt32 Cephei::QL::Instruments::CBond::SettlementDays::get ()
{
    try
    {
    	QuantLib::Natural _rv = (QuantLib::Natural)(*_ppBond)->settlementDays ( );   
        UInt32 _nrv = (UInt32)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CBond::SettlementValue (Double cleanPrice)
{
    try
    {
        QuantLib::Real _cleanPrice = (QuantLib::Real)ValueHelper::Convert (cleanPrice); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBond)->settlementValue ( _cleanPrice );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CBond::SettlementValue ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBond)->settlementValue ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CBond::Yield (Double cleanPrice, Cephei::QL::Times::IDayCounter^ dc, QL::CompoundingEnum comp, QL::Times::FrequencyEnum freq, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<Double>^ accuracy, Microsoft::FSharp::Core::FSharpOption<UInt64>^ maxEvaluations)
{
    CDayCounter^ _Cdc;
    try
    {
        QuantLib::Real _cleanPrice = (QuantLib::Real)ValueHelper::Convert (cleanPrice); //a
        _Cdc = safe_cast<CDayCounter^> (dc);
        _Cdc->Lock();
        QuantLib::DayCounter& _dc = static_cast<QuantLib::DayCounter&> (_Cdc->GetReference ()); 
        QuantLib::Compounding _comp = (QuantLib::Compounding)comp ;
        QuantLib::Frequency _freq = (QuantLib::Frequency)freq ;
        QuantLib::Date _settlementDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (settlementDate) ? (QuantLib::Date)ValueHelper::Convert (settlementDate->Value) : QuantLib::Date()); //9a
        QuantLib::Real _accuracy = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (accuracy) ? (QuantLib::Real)ValueHelper::Convert (accuracy->Value) : 1.0e-8); //9a
        QuantLib::Size _maxEvaluations = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (maxEvaluations) ? (QuantLib::Size)ValueHelper::Convert (maxEvaluations->Value) : 100); //9a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppBond)->yield ( _cleanPrice,  _dc,  _comp,  _freq,  _settlementDate,  _accuracy,  _maxEvaluations );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cdc != nullptr) _Cdc->Unlock();
    }
}
Double Cephei::QL::Instruments::CBond::Yield (Cephei::QL::Times::IDayCounter^ dc, QL::CompoundingEnum comp, QL::Times::FrequencyEnum freq, Microsoft::FSharp::Core::FSharpOption<Double>^ accuracy, Microsoft::FSharp::Core::FSharpOption<UInt64>^ maxEvaluations)
{
    CDayCounter^ _Cdc;
    try
    {
        _Cdc = safe_cast<CDayCounter^> (dc);
        _Cdc->Lock();
        QuantLib::DayCounter& _dc = static_cast<QuantLib::DayCounter&> (_Cdc->GetReference ()); 
        QuantLib::Compounding _comp = (QuantLib::Compounding)comp ;
        QuantLib::Frequency _freq = (QuantLib::Frequency)freq ;
        QuantLib::Real _accuracy = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (accuracy) ? (QuantLib::Real)ValueHelper::Convert (accuracy->Value) : 1.0e-8); //9a
        QuantLib::Size _maxEvaluations = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (maxEvaluations) ? (QuantLib::Size)ValueHelper::Convert (maxEvaluations->Value) : 100); //9a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppBond)->yield ( _dc,  _comp,  _freq,  _accuracy,  _maxEvaluations );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cdc != nullptr) _Cdc->Unlock();
    }
}
Double Cephei::QL::Instruments::CBond::PreviousCouponRate (Microsoft::FSharp::Core::FSharpOption<DateTime>^ d)
{
    try
    {
        QuantLib::Date _d = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (d) ? (QuantLib::Date)ValueHelper::Convert (d->Value) : QuantLib::Date()); //9a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppBond)->previousCouponRate ( _d );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Instruments::CBond::SettlementDate (Microsoft::FSharp::Core::FSharpOption<DateTime>^ d)
{
    try
    {
        QuantLib::Date _d = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (d) ? (QuantLib::Date)ValueHelper::Convert (d->Value) : QuantLib::Date()); //9a
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppBond)->settlementDate ( _d );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Instruments::CBond::StartDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppBond)->startDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::IBond^ Cephei::QL::Instruments::CBond_Factory::Create (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, Double faceAmount, DateTime maturityDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ issueDate, Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Cephei::QL::ICashFlow^>^>^ cashflows, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CBond ( settlementDays,  calendar,  faceAmount,  maturityDate,  issueDate,  cashflows,  QL_Pricer);
}
Cephei::QL::Instruments::IBond^ Cephei::QL::Instruments::CBond_Factory::Create (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, Microsoft::FSharp::Core::FSharpOption<DateTime>^ issueDate, Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Cephei::QL::ICashFlow^>^>^ coupons, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CBond ( settlementDays,  calendar,  issueDate,  coupons,  QL_Pricer);
}
